package toOffer;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2024/1/24 16:49
 * @description <p>有效的回文 </p>
 */

import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 题目：给定一个字符串，请判断它是不是回文。假设只需要考虑字母
 * 和数字字符，并忽略大小写。例如，" Was it a cat I saw?"是一个回文字符串，
 * 而"race a car"不是回文字符串。
 */
public class toOffer3_3_18 {
    public static void main(String[] args) {
        String s = "0P";
        System.out.println(isPalindrome(s));
    }

    /**
     * 双指针相遇
     * @param s
     * @return
     */
    public static boolean isPalindrome(String s) {
        //判空
        if (Objects.isNull(s) || s.length() == 0){
            return true;
        }

        //去除多余字符
        Pattern pattern = Pattern.compile("[^a-zA-Z0-9]");
        s = pattern.matcher(s).replaceAll("").toLowerCase();

        int left = 0;
        int right = s.length() - 1;

        while (left < s.length() || left < right){
            char start = s.charAt(left);
            char end = s.charAt(right);
            if (start != end){
                return false;
            }
            right--;
            left++;
        }

        return true;
    }
}
